package org.mclll.xwangplugin.questTracking.gui;

import com.google.common.collect.Lists;
import me.pikamug.quests.Quests;
import me.pikamug.quests.quests.Quest;
import net.kyori.adventure.text.Component;
import org.black_ixx.bossshop.BossShop;
import org.black_ixx.bossshop.core.BSBuy;
import org.black_ixx.bossshop.core.BSShop;
import org.black_ixx.bossshop.core.prices.BSPriceType;
import org.black_ixx.bossshop.core.rewards.BSRewardType;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.mclll.xwangplugin.MclllCustom;
import org.mclll.xwangplugin.log.GlobalLog;
import org.mclll.xwangplugin.log.LogUtil;
import org.mclll.xwangplugin.log.LoggerFactory;
import org.mclll.xwangplugin.questTracking.listener.QuestListener;
import org.mclll.xwangplugin.questTracking.persistence.Persistence;

import java.util.*;

/**
 * @author: geduo
 * @date: 2023/10/16 10:33
 */
public class BossShopMenuController {

    private static LogUtil log = LoggerFactory.get(GlobalLog.class);

    public static void openQuestTrackingMenu(Player player, MclllCustom plugin) {
        BossShop bs = (BossShop) Bukkit.getPluginManager().getPlugin("BossShopPro");
        if (bs == null) {
            return;
        }
        BSShop shop = bs.getAPI().getShop(QuestListener.SCOREBOARD_NAME);
        loadQuests(bs, player, shop, plugin);
        bs.getAPI().openShop(player, shop);
    }

    public static void loadQuests(BossShop bs, Player player, BSShop shop, MclllCustom plugin) {
        UUID playerUuid = player.getUniqueId();
        Map<String, Integer> stringIntegerMap = QuestListener.PLAYER_QUEST_STAGE_MAP.get(playerUuid);
        // 先清理
        shop.getItems().clear();
        if (stringIntegerMap == null || stringIntegerMap.isEmpty()) {
            // 添加一张空纸
            ItemStack itemStack = new ItemStack(Material.PAPER);
            itemStack.lore(Arrays.asList(Component.text("&7看来你并没有接取任何任务！"), Component.text("&7寻找城镇上的NPC找他们接取任务吧！")));
            ItemMeta itemMeta = itemStack.getItemMeta();
            itemMeta.displayName(Component.text("&c暂无任务"));
            itemStack.setItemMeta(itemMeta);
            BSBuy bsBuy = bs.getAPI().createBSBuy(BSRewardType.Nothing, BSPriceType.Nothing, null, null, null, 13, null);
            shop.addShopItem(bsBuy, itemStack, bs.getClassManager());
        } else {
            // 再更新
            int index = 0;
            for (String questsName : stringIntegerMap.keySet()) {
                ItemStack itemStack = new ItemStack(Material.BOOK);
                ItemMeta itemMeta = itemStack.getItemMeta();
                itemMeta.displayName(Component.text("&a" + questsName));
                itemStack.setItemMeta(itemMeta);
                Quests pluginPlugin = (Quests) Bukkit.getPluginManager().getPlugin("Quests");
                Quest quest = null;
                for (Quest loadedQuest : pluginPlugin.getLoadedQuests()) {
                    if (questsName.equals(loadedQuest.getName())) {
                        quest = loadedQuest;
                        break;
                    }
                }
                if (quest == null) {
                    continue;
                }
                List<Component> lore = Lists.newArrayList();
                String description = quest.getDescription();
                for (int i = 0; i < description.length(); i += 30) {
                    int endIndex = Math.min(i + 30, description.length());
                    String subStr = description.substring(i, endIndex);
                    lore.add(Component.text("&7&o" + subStr));
                }
                lore.add(Component.text("&f目标:"));
                List<Persistence.SerializeScore> scoreList = QuestListener.QUEST_SCOREBOARD_MAP.get(playerUuid).get(questsName);
                if (scoreList == null || scoreList.isEmpty()) {
                    QuestListener.fixQuestsTracking(player);
                    scoreList = QuestListener.QUEST_SCOREBOARD_MAP.get(playerUuid).get(questsName);
                }
                for (int i = 1; i < scoreList.size(); i++) {
                    lore.add(Component.text(scoreList.get(i).getName()));
                }
                lore.add(Component.text("&e◆ 点击追踪任务"));
                itemStack.lore(lore);
                BSBuy bsBuy = bs.getAPI().createBSBuy(BSRewardType.PlayerCommand, BSPriceType.Nothing, Collections.singletonList("poirw " + questsName), null, null, index++, null);
                shop.addShopItem(bsBuy, itemStack, bs.getClassManager());
            }
        }
        // 添加返回按钮
        ItemStack itemStack = new ItemStack(Material.ARROW);
        itemStack.lore(Collections.singletonList(Component.text("&7点击返回主菜单")));
        ItemMeta itemMeta = itemStack.getItemMeta();
        itemMeta.displayName(Component.text("&a◆ 返回_"));
        itemStack.setItemMeta(itemMeta);
        BSBuy bsBuy = bs.getAPI().createBSBuy(BSRewardType.Shop, BSPriceType.Nothing, "menu", null, null, 26, null);
        shop.addShopItem(bsBuy, itemStack, bs.getClassManager());
    }


}
